package com.itheima.deadlock;

import java.io.ObjectInputStream;

public class Demo01 {

    public static void main(String[] args) throws InterruptedException {
        Object suoA = new Object();
        Object suoB = new Object();

        Thread t1 = new Thread(() -> {
            while (true) {
                synchronized (suoA){
                    System.out.println("尔康进入了A锁");
                    synchronized (suoB){
                        System.out.println("尔康进入了B锁");
                        System.out.println("尔康要出去了");
                    }
                    System.out.println("尔康出去了B锁");
                    System.out.println("尔康要出去A锁le");
                }
                System.out.println("尔康从A锁出来了");
            }

        });
        t1.setName("尔康");

        Thread t2 = new Thread(() -> {
            String name = Thread.currentThread().getName();
            while (true) {
                synchronized (suoB){
                    System.out.println(name+"进入了B锁");
                    synchronized (suoA){
                        System.out.println(name+"进入了A锁");
                        System.out.println(name+"要出去了A");
                    }
                    System.out.println(name+"出去了A锁");
                    System.out.println(name+"要出去B锁le");
                }
                System.out.println(name+"从B锁出来了");
            }
        });
        t2.setName("紫薇");
        t1.start();
        Thread.sleep(10);
        t2.start();

    }
}
